package cn.duhengkui.national.valuelist.context;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;

import cn.duhengkui.national.valuelist.core.data.CommandExecutor;
import cn.duhengkui.national.valuelist.core.data.ReadOnlyDao;
import cn.duhengkui.national.valuelist.core.data.StandardDao;
import cn.duhengkui.national.valuelist.core.data.aop.FinderIntroductionInterceptor;
import cn.duhengkui.national.valuelist.tool.ClassPathScanHandler;



/**   
 * <table border="1">
 *	<tr><td>包名称：</td><td>cn.duhengkui.national.valuelist.context</td></tr>
 *	<tr><td>类名称：</td><td>AddDaoParentNameProcessor</td></tr>
 *	<tr><td>类描述：</td><td>给DAO增加parent属性</td></tr>
 *	<tr><td>创建人：</td><td>杜恒奎</td></tr>
 *  <tr><td>创建时间：</td><td>2018年6月7日/下午2:58:45</td></tr> 
 *  <tr><td>版本：</td><td>1.0.0</td></tr>
 * </table>   
 *     
 */
public class AddDaoParentNameProcessor implements BeanFactoryPostProcessor {

	//private @Resource String daosPackage;
	
	protected static final Logger logger = LoggerFactory.getLogger(AddDaoParentNameProcessor.class);
	
	private @Resource String parentDao;

	/*public void setDaosPackage(String daosPackage) {
		this.daosPackage = daosPackage;
	}*/

	public void setParentDao(String parentDao) {
		this.parentDao = parentDao;
	}


	public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
		ClassPathScanHandler scanHandler = new ClassPathScanHandler(true, true, new ArrayList<String>());
		//String[] daop = daosPackage.split(",");
		//for(String daoPackage : daop) {
		logger.info("Beging add dao parent...");
			Set<Class<?>> classSet = scanHandler.getPackageAllClasses("", true,false);
			for(Class<?> clazz : classSet) {
				if(clazz.isInterface() && isDao(clazz)) {
					BeanDefinition beanDefinition = new GenericBeanDefinition();
					beanDefinition.setParentName(parentDao);
					PropertyValue propertyValue = new PropertyValue("interfaces", clazz);
					List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>(1);
					propertyValueList.add(propertyValue);
					MutablePropertyValues propertyValues = new MutablePropertyValues(propertyValueList);
					((AbstractBeanDefinition)beanDefinition).setPropertyValues(propertyValues);
					((AbstractBeanDefinition)beanDefinition).setLazyInit(true);
					((DefaultListableBeanFactory)beanFactory).registerBeanDefinition(clazz.getName(), beanDefinition);
				}
			}
		//}
	}
	
	private boolean isDao(Class<?> clazz) {
		Class<?>[] interfaces = clazz.getInterfaces();
		if(interfaces == null || interfaces.length == 0) {
			return false;
		}
		for(Class<?> interfaceClazz : interfaces) {
			if(interfaceClazz.equals(StandardDao.class) || interfaceClazz.equals(ReadOnlyDao.class) || interfaceClazz.equals(CommandExecutor.class)) {
				return true;
			}
		}
		return false;
	}

}
